package com.itheima.heima.info.manager.system.dao;

import com.itheima.heima.info.manager.system.domain.Student;
import com.itheima.heima.info.manager.system.domain.Teacher;

/*
老师操作数据类
老师的增删改查
写在这个类中
 */
public class TeacherDao {

    //数据存储在哪
    private static Teacher[] teas=new Teacher[10];
    static {
        Teacher cai = new Teacher("0001", "蔡徐坤", 25, "1996");
        Teacher lu = new Teacher("0002", "鹿晗", 30, "1996");
        Teacher huang = new Teacher("0003", "黄子韬", 28, "1996");
        Teacher wu = new Teacher("0004", "吴亦凡", 28, "1996");
        teas[0]=cai;
        teas[1]=lu;
        teas[2]=huang;
        teas[3]=wu;

    }

    //增 参数 Teacher teacher 返回值 boolean
    public boolean addTeacher(Teacher teacher){
        for (int i = 0; i < teas.length; i++) {
            //teas[i]
            Teacher tea = teas[i];
            if(tea==null){
                teas[i]=teacher;
                return true;
            }
        }
        return false;
    }
    //删参数  String sid 返回值void
    public void deleteTeacher(String sid){
        //根据sid 找索引
        int index = getIndex(sid);
        //把索引上的值替换为null
        teas[index]=null;
    }

    //改 参数  String sid  Teacher teacher  返回值 void
    public void updateTeacher(String sid,Teacher teacher){
        //根据sid 找索引
        int index = getIndex(sid);
        //把索引上的值替换为新的老师
        teas[index]=teacher;
    }
    //查 参数没有  返回值 所有的老师
    public Teacher[] findAll(){

        return teas;
    }
    //根据条件查
    //参数 Sting sid 返回值 int
    public int getIndex(String sid){
        int index=-1;
        for (int i = 0; i < teas.length; i++) {
            //teas[i]
            Teacher tea = teas[i];
            if(tea!=null&&tea.getSid().equals(sid)){
                index=i;
                break;
            }
        }
        return index;


    }
}
